var category = {
	originalPage : 'pages/setting.html',
	show : function(transition,originalPage) {
		var db = app.getDB();
		db.transaction(function(tx) {
			tx.executeSql('select * from category order by sort', [], function(tx, results) {
				var len = results.rows.length;
				var template = $.createTemplateURL('templates/categorylist.html');
				if(originalPage){
					category.category = originalPage;
				} 
				template.setParam("originalPage", category.category);	
				var data = [];
				for (var i = 0; i < len; i++) {
					data.push(results.rows.item(i));
				}
				var html = template.get(data);
				app.loadHTML(html,{
					transition :transition
				},null)

			}, errorCB);

		}, errorCB);

	},
	showDetail : function(category, transtion) {
		var db = app.getDB();
		db.transaction(function(tx) {
			tx.executeSql('SELECT * FROM MENU WHERE CATEGORY_ID =? ORDER BY NAME ', [category.id],
			function(tx, results) {
				var len = results.rows.length;
				var template = $.createTemplateURL('templates/category_detail.html');
				var foodList = [];
				for (var i = 0; i < len; i++) {
					foodList.push(results.rows.item(i));
				}
				var html = template.get({
					foodList : foodList,
					info : category
				});
				app.loadHTML(html, {
					transition : transtion
				});

			}, errorCB);

		}, errorCB);

	},
	showAdd : function() {
		app.loadPage('pages/category_add.html', {
			transition : 'pop-in'
		})
	},
	add : function(name, sort) {
		var db = app.getDB();
		db.transaction(function(tx) {
			tx.executeSql('insert into category (name,sort) VALUES(?,?)', [name, sort], function(tx, results) {
				category.show('slide-right');
			}, errorCB);

		}, errorCB);
	},
	showUpdate : function(category, transtion) {
		var template = $.createTemplateURL('templates/category_edit.html');
		var html = template.get(category);
		app.loadHTML(html, {
			transition : transtion
		})
	},
	update : function(name, sort, id) {
		var db = app.getDB();
		db.transaction(function(tx) {
			tx.executeSql('update category set name =? , sort = ? where id = ?', [name, sort, id],
			function(tx, results) {
				category.showDetail({
					id : id,
					name : name,
					sort : sort
				}, 'slide-right');
			}, errorCB);

		}, errorCB);
	},
	deleteObj : function(categoryInfo){
		app.msg.comfirm("提示","删除后其对应的菜谱也会被删除,确定要删除该菜系吗?",function(){
			var db = app.getDB();
			db.transaction(function(tx) {
				tx.executeSql('delete from  menu where category_id=? ', [categoryInfo.id]);
				tx.executeSql('delete from  category where id=? ', [categoryInfo.id]);
			}, errorCB,function(){
				jsTouch.overlayClose();
				category.show('slide-right');
			});
		},function(){
			jsTouch.overlayClose();
		})
	}

};